package com.example.demo.mapper;

import com.example.demo.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;
import java.util.List;

/**
* @author 三石
* @description 针对表【user(系统用户)】的数据库操作Mapper
* @createDate 2024-05-06 21:39:48
* @Entity com.example.demo.domain.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 根据用户名查询密码
     * @param username 用户名
     * @return 密码
     */
    @Select("select password from user where username = #{username}")
    String selectPasswordByUsername(@Param("username") String username);

    /**
     * 根据用户名查询角色id集合
     * @param username 用户名
     * @return 角色id集合
     */

    /**
     * 根据用户名查询role_ids
     * @param username 用户名
     * @return role_ids
     */
    @Select("select role_id from user left join user_role on user.id =user_role.user_id where username = #{username}")

    List<Long> selectRoleIdsByUsername(@Param("username") String username);

    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    @Select("select * from user where username = #{username}")
    User selectByUserName(@Param("username") String username);

    /**
     * 根据角色id集合查询用户角色名
     * @param roleIds 角色id
     * @return
     */
    @Select(("<script>"+
            "SELECT  rolename FROM role " +
            "WHERE  id IN " +
            "<foreach collection='roleIds' item='roleId' open='(' close=')' separator=','>" +
            "#{roleId}" +
            "</foreach>" +
            "</script> "))
    ArrayList<String> getRoleByIds(@Param("roleIds") List<Long> roleIds);
}




